package com.gest.util;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Dates {
	// * Choix de la langue francaise
	static Locale locale = Locale.getDefault();
	static Date actuelle = new Date(0);

	// * Definition du format utilise pour les dates
	static DateFormat dateFormatDYY = new SimpleDateFormat("dd/MM/yyyy");
	static DateFormat dateFormatHMS = new SimpleDateFormat("hh:mm:ss");
	static DateFormat dateYear = new SimpleDateFormat("yyyy");

	// * Donne la date au format "aaaa-mm-jj"
	public static String date() {
		String dat = dateFormatDYY.format(actuelle);
		return dat;
	}

	public static String Year() {
		String dat = dateYear.format(actuelle);
		return dat;
	}

	public static String DateEtHeurEtMinEtSecEnString() {
		String result = dateFormatDYY.format(actuelle)
				+ dateFormatHMS.format(actuelle);
		return result;
	}

	public static String getTodayDate() {
		int month = java.util.Calendar.getInstance().get(
				java.util.Calendar.MONTH);
		int year = java.util.Calendar.getInstance()
				.get(java.util.Calendar.YEAR);
		int day = java.util.Calendar.getInstance().get(
				java.util.Calendar.DAY_OF_MONTH);
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"dd-MM-yyy");
		java.util.Calendar cal = java.util.Calendar.getInstance();
		cal.set(year, month, day);
		return sdf.format(cal.getTime());
	}

	public static Date stringToDate(String sDate) {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"dd-MM-yyyy");
		java.util.Date utilDate = new java.util.Date();
		try {
			utilDate = formatter.parse(sDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		java.sql.Date sqlDate = new Date(0);
		sqlDate = new java.sql.Date(utilDate.getTime());
		return sqlDate;
	}

	public static Timestamp toDBDateFormat(String date) {
		if (date == null)
			return null;
		int day = Integer.parseInt(date.substring(3, 5));
		int moi = Integer.parseInt(date.substring(0, 2));
		int annee = Integer.parseInt(date.substring(6, 10));
		int heure = Integer.parseInt(date.substring(11, 13));
		int minute = Integer.parseInt(date.substring(14, 16));
		int seconde = Integer.parseInt(date.substring(17, 19));
		Timestamp t = new Timestamp(annee - 1900, moi - 1, day, heure, minute,
				seconde, 0);
		return t;
	}

	public static String ChangerFormatDateString(java.util.Date d,
			String NEW_FORMAT) {
		SimpleDateFormat sdf = new SimpleDateFormat(NEW_FORMAT);
		String DateString = sdf.format(d);
		return DateString;
	}
}